package com.example.myapplication.tack_30.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_30.Details02Activity;
import com.example.myapplication.tack_30.DetailsActivity;
import com.example.myapplication.tack_30.ThemeActivity;
import com.example.myapplication.tack_30.data.KeywordData;
import com.example.myapplication.tack_30.data.NearData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class KeywordAdapter extends RecyclerView.Adapter<KeywordAdapter.My> {
    private List<KeywordData.RowsDTO> list;
    private Context context;
    private View view;
    private Gson gson;
    public KeywordAdapter(List<KeywordData.RowsDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_30_near,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL+list.get(position).getImgUrl()).into(holder.imgUrl);
        holder.name.setText(list.get(position).getName());
        holder.score.setText(list.get(position).getScore()+"分");
        holder.distance.setText("到店距离:"+list.get(position).getDistance()+"m");
        holder.deliveryTime.setText("配送时间"+list.get(position).getDeliveryTime()+"/分");
        holder.avgCost.setText("人均消费:"+list.get(position).getAvgCost());

        holder.line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if( context instanceof ThemeActivity){
                    Intent intent = new Intent(context, Details02Activity.class);
                    gson = new Gson();
                    String json = gson.toJson(list.get(position));
                    intent.putExtra(Common.COMMON,json);
                    context.startActivity(intent);
//                }else{
//                    Intent intent = new Intent(context, DetailsActivity.class);
//                    gson = new Gson();
//                    String json = gson.toJson(list.get(position));
//                    intent.putExtra(Common.COMMON,json);
//                    context.startActivity(intent);
//                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView imgUrl;
        private TextView name;
        private TextView score;
        private TextView distance;
        private TextView deliveryTime;
        private TextView avgCost;
        private LinearLayout line;
        public My(@NonNull View itemView) {
            super(itemView);
            imgUrl = view.findViewById(R.id.imgUrl);
            name = view.findViewById(R.id.name);
            score = view.findViewById(R.id.score);
            distance = view.findViewById(R.id.distance);
            deliveryTime = view.findViewById(R.id.deliveryTime);
            avgCost = view.findViewById(R.id.avgCost);
            line = view.findViewById(R.id.line);
        }
    }
}
